using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using NCS.Library.Components;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Providers;
using System.Data.SqlClient;

namespace NCS.Module.CMS.Library.Events
{
    public class EventController
    {
        private const string key = "NCS.Portal.CMS.Event";
        public static DataTable GetAll()
        {
            string param = "Event_GetAll?portalid=" + AppEnv.PortalId();
            DataCaching data = new DataCaching();
            DataTable dtEvents = (DataTable)data.GetHashCache(key, param);
            if (dtEvents != null) return dtEvents;
            dtEvents = DataAccessProvider.Instance().EventGetAll();
            data.SetHashCache(key, param, Constants.CacheExpire_Content, dtEvents);
            return dtEvents;
        }
        public static void SetPriority(int eventid, int priority)
        {
            DataAccessProvider.Instance().SetPriority(eventid, priority);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
        }
        public static DataTable GetAllByNum(int num)
        {
            string param = "GetAllByNum?portalid=" + AppEnv.PortalId() + "&num=" + num;
            DataCaching data = new DataCaching();
            DataTable dtEvents = (DataTable)data.GetHashCache(key, param);
            if (dtEvents != null) return dtEvents;
            dtEvents = DataAccessProvider.Instance().EventGetAllByNum(num);
            data.SetHashCache(key, param, Constants.CacheExpire_Content, dtEvents);
            return dtEvents;
        }
        public static EventInfo GetInfo(int eventid)
        {
            EventInfo info;
            info = DataAccessProvider.Instance().Event_GetInfo(eventid);
            return info;
        }
        public static void Event_SetDistributionPriority(int eventid, int distid, int newPriority)
        {
            DataAccessProvider.Instance().Event_SetDistributionPriority(eventid, distid, newPriority);
        }
        public static int Event_GetEventIDByDistID(int distid)
        {
            return DataAccessProvider.Instance().Event_GetEventIDByDistID(distid);
        }
        public static DataTable Event_GetNewsByEventID(int eventid, int numberRecord)
        {
            string param = "Event_getnewsbyID?eventid=" + eventid;
            DataCaching dc = new DataCaching();
            DataTable dtevent = (DataTable)dc.GetHashCache(key, param);
            if (dtevent != null) return dtevent;
            dtevent = DataAccessProvider.Instance().Event_GetNewsByEventID(eventid, numberRecord);
            int contentid = 0;
            for (int i = 0; i < dtevent.Rows.Count; i++)
            {
                if (dtevent.Rows[i]["Content_ID"].ToString() != contentid.ToString())
                    contentid = Convert.ToInt32(dtevent.Rows[i]["Content_ID"]);
                else
                    dtevent.Rows.RemoveAt(i);
            }

            dc.SetHashCache(key, param, Constants.CacheExpire_Content, dtevent);

            return dtevent;
        }
        /// <summary>
        /// Lay Ds bai viet theo nhom su kieb
        /// </summary>
        /// <param name="eventId">ma su kien</param>
        /// <returns></returns>
        public static DataTable GetNewsByEventID(int eventId)
        {
            DataTable retVal = null;

            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("CMS_Event_GetNewsByEventGroup", dbConn);
            dbCmd.Parameters.AddWithValue("@Event_ID", eventId);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }

            return retVal;
        }

        /// <summary>
        /// Kiem tra xem bai viet da co trong nhom su kien chua?
        /// </summary>
        /// <param name="eventId">ma su kien</param>
        /// <param name="newsID">ma bai viet</param>
        /// <returns></returns>
        public static DataTable CheckExistNewsByEventID(int eventId, int newsID)
        {
            DataTable retVal = null;
           
                SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
                SqlCommand dbCmd = new SqlCommand("CMS_Event_CheckExistNewsInEventGroup", dbConn);
                dbCmd.Parameters.AddWithValue("@Distribution_Id", newsID);
                dbCmd.Parameters.AddWithValue("@Event_ID", eventId);
                dbCmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    dbConn.Open();
                    retVal = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                    da.Fill(retVal);
                }
                finally
                {
                    dbConn.Close();
                }
               
            
            return retVal;
        }
        public static DataTable Event_GetNewsByEventIDExcludeDistid(int eventid, int numberRecord, int distid)
        {
            //string param = "Event_GetNewsByEventIDExcludeDistid?eventid=" + eventid + "&Distid=" + distid;
            //DataCaching dc = new DataCaching();
            DataTable dtevent ;//= (DataTable)dc.GetHashCache(key, param);
            //if (dtevent != null) return dtevent;
            dtevent = DataAccessProvider.Instance().Event_GetNewsByEventIDExcludeDistID(eventid, numberRecord, distid);
            //dc.SetHashCache(key, param, Constants.CacheExpire_Content, dtevent);
            int contentid = 0;
            for (int i = 0; i < dtevent.Rows.Count; i++ )
            {
                if (dtevent.Rows[i]["Content_ID"].ToString() != contentid.ToString())
                    contentid = Convert.ToInt32(dtevent.Rows[i]["Content_ID"]);
                else
                    dtevent.Rows.RemoveAt(i);
            }
            return dtevent;
        }
        public static EventInfo Event_GetEventInfoByDistID(int distid)
        {
            EventInfo info;
            info = DataAccessProvider.Instance().Event_GetEventInfoByDistID(distid);
            return info;
        }
        public static void Event_AddDistribution(int eventID, int distid)
        {
            DataAccessProvider.Instance().Event_AddDistribution(eventID, distid);
        }
        public static void Event_RemoveDistribution(int eventID, int distid)
        {
            DataAccessProvider.Instance().Event_RemoveDistribution(eventID, distid);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
        }
        public static void Event_Delete(int eventID)
        {
            DataAccessProvider.Instance().Event_Delete(eventID);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
        }
        public static int Event_Insert(EventInfo einfo)
        {
            int i = 0;
            i = DataAccessProvider.Instance().Event_Insert(einfo);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
            return i;
        }
        public static void Event_Update(EventInfo einfo)
        {
            DataAccessProvider.Instance().Event_Update(einfo);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
        }
    }
}
